﻿/*  
 * Helix Engine
 * http://helixengine.codeplex.com/
 * 
 * Copyright (c) 2010 - 2011 Joe Hegarty
 *  
 * This source code is subject to the terms of the Microsoft Public License (Ms-PL). 
 *  
 * Redistribution and use in source and binary forms, with or without modification, 
 * is permitted provided that redistributions of the source code retain the above 
 * copyright notices and this file header. 
 * 
 * For details please see http://www.opensource.org/licenses/ms-pl.html 
 *  
 * All other rights reserved. 
 *
 */  


using System;

namespace HelixEngine.Maths
{
    /// <summary>
    /// 
    /// </summary>
    public static class IsometricHelper
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="isoPoint"></param>
        /// <returns></returns>
        public static Vector2 WorldSpaceToScreenSpace(IsometricPoint isoPoint)
        {
            Vector2 result = new Vector2();
            result.X = (IsometricSettings.TileWidth / 2) * (isoPoint.Y - isoPoint.X) + IsometricSettings.OffsetWidth;
            result.Y = (IsometricSettings.TileHeight / 2) * (isoPoint.Y + isoPoint.X) + IsometricSettings.OffsetHeight;
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="isoPoint"></param>
        /// <param name="itemWidth"></param>
        /// <param name="itemHeight"></param>
        /// <returns></returns>
        public static Vector2 WorldSpaceToScreenSpace(IsometricPoint isoPoint, float itemWidth, float itemHeight)
        {
            Vector2 result = WorldSpaceToScreenSpace(isoPoint);
            result.X = result.X + (IsometricSettings.TileWidth - itemWidth);
            result.Y = result.Y + (IsometricSettings.TileHeight - itemHeight);
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="isoPoint"></param>
        /// <param name="itemWidth"></param>
        /// <param name="itemHeight"></param>
        /// <returns></returns>
        public static Vector2 WorldSpaceToScreenSpace(IsometricPoint isoPoint, double itemWidth, double itemHeight)
        {
            return WorldSpaceToScreenSpace(isoPoint, (float)itemWidth, (float)itemHeight);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="screenSpace"></param>
        /// <returns></returns>
        public static IsometricPoint ScreenSpaceToWorldSpace(Vector2 screenSpace)
        {
            double offsetX = (screenSpace.X - IsometricSettings.OffsetWidth) / (IsometricSettings.TileWidth / 2);
            double offsetY = (screenSpace.Y - IsometricSettings.OffsetHeight) / (IsometricSettings.TileHeight / 2);
            IsometricPoint result = new IsometricPoint();
            result.X = (int)System.Math.Round((offsetY - offsetX) / 2);
            result.Y = (int)System.Math.Round((offsetY + offsetX) / 2) - 1;
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="isoPoint"></param>
        /// <returns></returns>
        public static int CalculateTileDepth(IsometricPoint isoPoint)
        {
            return (isoPoint.Y * IsometricSettings.TileHeight) + (isoPoint.X * IsometricSettings.TileWidth) + isoPoint.Y;
        }
    }
}
